package com.turbo.boot.crawler.spider.worker.impl.quota;

import com.alibaba.fastjson.JSON;
import com.turbo.boot.crawler.dao.dal.CrawlerResultDal;
import com.turbo.boot.crawler.dao.entity.CrawlerResultEntity;
import com.turbo.boot.crawler.spider.worker.BaseTurboCrawlerPageParser;
import com.xuxueli.crawler.annotation.PageFieldSelect;
import com.xuxueli.crawler.annotation.PageSelect;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 描述
 *
 * @author wuyn
 * @since 2024/4/19
 */
@Service("sinaQuotaFx678CrawlerPageParser")
public class SinaQuotaFx678CrawlerPageParser extends BaseTurboCrawlerPageParser<SinaQuotaFx678CrawlerPageParser.PageVo> {


    @Resource
    private CrawlerResultDal crawlerResultDal;


    @Override
    public void parse(Document html, Element pageVoElement, PageVo pageVo) {
        // 解析封装 PageVo 对象
        String pageUrl = html.baseUri();
        if (StringUtils.isEmpty(pageVo.getConc())) {
            return;
        }
        System.out.println(pageUrl + "：" + pageVo.toString());
        // 处理业务代码


        List<CrawlerResultEntity> batch = new ArrayList<>();

        if (StringUtils.isNotBlank(pageVo.getConc())) {
            CrawlerResultEntity conc = buildCrawlerResultEntity();
            conc.setCollectedAt(new Date());
            conc.setQuotaCode("CONC");
            conc.setQuotaName("美国原油连续");
            conc.setQuotaValue(pageVo.getConc());
            batch.add(conc);
        }

        if (StringUtils.isNotBlank(pageVo.getOilc())) {
            CrawlerResultEntity oilc = buildCrawlerResultEntity();
            oilc.setCollectedAt(new Date());
            oilc.setQuotaCode("OILC");
            oilc.setQuotaName("布伦特原油连续");
            oilc.setQuotaValue(pageVo.getOilc());

            batch.add(oilc);
        }


        if (StringUtils.isNotBlank(pageVo.getSc0001())) {
            CrawlerResultEntity sc0001 = buildCrawlerResultEntity();
            sc0001.setCollectedAt(new Date());
            sc0001.setQuotaCode("SC0001");
            sc0001.setQuotaName("原油连续");
            sc0001.setQuotaValue(pageVo.getSc0001());

            batch.add(sc0001);
        }

        if (StringUtils.isNotBlank(pageVo.getHonc())) {
            CrawlerResultEntity honc = buildCrawlerResultEntity();
            honc.setCollectedAt(new Date());
            honc.setQuotaCode("HONC");
            honc.setQuotaName("美国燃油连续");
            honc.setQuotaValue(pageVo.getHonc());

            batch.add(honc);
        }

        if (StringUtils.isNotBlank(pageVo.getGalc())) {
            CrawlerResultEntity galc = buildCrawlerResultEntity();
            galc.setCollectedAt(new Date());
            galc.setQuotaCode("GALC");
            galc.setQuotaName("伦敦柴油连续");
            galc.setQuotaValue(pageVo.getGalc());

            batch.add(galc);
        }


        if (CollectionUtils.isNotEmpty(batch)) {
            crawlerResultDal.insertBatch(batch);
        }
    }


    @Setter
    @Getter
    @PageSelect(cssQuery = ".quoteIdx_table")
    public static class PageVo {

        @PageFieldSelect(cssQuery = "#code_CONC > td.code_CONC_c")
        private String conc;

        @PageFieldSelect(cssQuery = "#code_OILC > td.code_OILC_c")
        private String oilc;

        @PageFieldSelect(cssQuery = "#code_SC0001 > td.code_SC0001_c")
        private String sc0001;

        @PageFieldSelect(cssQuery = "#code_HONC > td.code_HONC_c")
        private String honc;


        @PageFieldSelect(cssQuery = "#code_GALC > td.code_GALC_c")
        private String galc;


        @Override
        public String toString() {
            return JSON.toJSONString(this);
        }
    }
}
